/*
 * Copyright 2002-2006 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and limitations under the
 * License.
 */
package pikes.html.xhtml.form;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import pikes.core.Printable;
import pikes.html.xhtml.Block;
import pikes.html.xhtml.CoreAttributes;
import pikes.html.xhtml.Events;
import pikes.html.xhtml.Flow;
import pikes.html.xhtml.I18N;
import pikes.html.xhtml.NonRootTagFactory;
import pikes.html.xhtml.Script;
import pikes.uri.RelativeURI;
import pikes.uri.URI;
import pikes.util.Assert;
import pikes.xml.XMLTag;

/**
 * <code>&lt;form/&gt;</code> tag, see <a href="http://www.w3.org/TR/html401/interact/forms.html#h-17.3">FORM element</a>
 * in HTML specification.
 * @author Peter Bona
 * @see <a href="http://www.w3.org/TR/html401/interact/forms.html#h-17.3">The FORM element in HTML specification</a>
 */
public class Form extends NonRootTagFactory implements Block, CoreAttributes, I18N, Events {

	public interface Method extends Printable {

		public Method GET = new Method() {

			@Override
			public String toString() {
				return "get";
			}

			public void print(Appendable appendable) throws IOException {
				appendable.append("get");
			}

		};

		public Method POST = new Method() {

			@Override
			public String toString() {
				return "post";
			}

			public void print(Appendable appendable) throws IOException {
				appendable.append("post");
			}
		};
	}

	private URI action = null;

	private Method method = null;

	private List<Flow> flows = new LinkedList<Flow>();

	public Form(CharSequence action) {
		this(new RelativeURI(action));
	}

	public Form(URI action) {
		Assert.notNull(action);
		this.action = action;
	}

	public Form(CharSequence action, Method method) {
		this(action);
		this.method = method;
	}

	public Form(URI action, Method method) {
		this(action);
		this.method = method;
	}

	/**
	 * @return "form"
	 */
	@Override
	protected CharSequence getTagName() {
		return "form";
	}

	public void add(Block block) {
		if (block != null) {
			flows.add(block);
		}
	}

	public void add(Script script) {
		if (script != null) {
			flows.add(script);
		}
	}

	@Override
	protected void decorateCreatedTag(XMLTag tag) {
		if (action != null) {
			tag.addAttribute("action", action);
		}

		if (method != null) {
			tag.addAttribute("method", method);
		}

		for (Flow flow : flows) {
			flow.decorateTag(tag);
		}
	}

}
